﻿using System;

namespace Transformable_Engine_v2.Engine.Animation.Trajectory
{
    public class QuadraticEasing : BaseTrajectory
    {
        private readonly Easing _easing;

        public QuadraticEasing(Easing easing)
        {
            _easing = easing;
        }


        public override double GetPercentageProgression(double elapsedDurationTime)
        {
            switch (_easing)
            {
                case Easing.EaseIn:
                    return GetEaseIn(elapsedDurationTime);
                case Easing.EaseOut:
                    return GetEaseOut(elapsedDurationTime);
                case Easing.EaseInAndOut:
                    return GetEaseInAndOut(elapsedDurationTime);
                default:
                    throw new ArgumentOutOfRangeException();
            }
        }

        private double GetEaseOut(double elapsedDurationTime)
        {
            return -elapsedDurationTime*(elapsedDurationTime - 2);
        }

        private double GetEaseIn(double elapsedDurationTime)
        {
            return elapsedDurationTime*elapsedDurationTime;
        }

        private double GetEaseInAndOut(double elapsedDurationTime)
        {
            if (elapsedDurationTime < 0.5)
                return 0.5 * Math.Pow(2 * elapsedDurationTime, 2);

            return -0.5 * ((2 * (elapsedDurationTime - 0.5)) * (2 * (elapsedDurationTime - 0.5) - 2) - 1);
            
        }
    }
}